﻿using System;
using System.Collections.Generic;
using Ants.SimpleDistributionProblem.DTO;

namespace Ants.SimpleDistributionProblem.AntClasses
{
    /// <summary>
    /// Class calculating probability of route parts.
    /// </summary>
    public class RoutePartWithPropabilityProvider
    {
        /// <summary>
        /// Gets the route parts with probability.
        /// </summary>
        /// <param name="routeParts">The route parts.</param>
        /// <param name="alpha">The alpha.</param>
        /// <param name="beta">The beta.</param>
        /// <returns>The route parts with probability.</returns>
        public List<RoutePartWithPropability> GetRoutePartsWithPropability(List<RoutePart> routeParts, double alpha, double beta)
        {
            List<RoutePartWithPropability> result = new List<RoutePartWithPropability>();
            double nominative = 0;
            foreach (RoutePart part in routeParts)
            {
                nominative += (Math.Pow(part.PheromoneAmount, alpha) * Math.Pow(this.Attractiveness(part), beta));
            }

            foreach (RoutePart part in routeParts)
            {
                result.Add(new RoutePartWithPropability()
                    {
                        PheromoneAmount = part.PheromoneAmount,
                        PointA = part.PointA,
                        PointB = part.PointB,
                        Probability = (Math.Pow(part.PheromoneAmount, alpha) * Math.Pow(Attractiveness(part), beta)) / nominative
                    });
            }

            return result;
        }

        /// <summary>
        /// Method which calculates attractiveness the specified route part.
        /// </summary>
        /// <param name="routePart">The route part.</param>
        /// <returns>
        /// The attractiveness of route part.
        /// </returns>
        private double Attractiveness(RoutePart routePart)
        {
            return 1 / routePart.Cost;
        }
    }
}